package com.sjy.ylxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.dto.HandleComplaintsDTO;
import com.sjy.ylxt.entity.dto.QueryComplaintsDTO;
import com.sjy.ylxt.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.sjy.ylxt.entity.domain.YlxtComplaints;
import com.sjy.ylxt.mapper.YlxtComplaintsMapper;
import com.sjy.ylxt.service.YlxtComplaintsService;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: 史俊颖
* @CreateTime: 2024-12-12
* @Description: ${description}
*/
@Service
@Slf4j
public class YlxtComplaintsServiceImpl extends ServiceImpl<YlxtComplaintsMapper, YlxtComplaints> implements YlxtComplaintsService{

    @Resource
    private YlxtComplaintsMapper ylxtComplaintsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ylxtComplaintsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(YlxtComplaints record) {
        return ylxtComplaintsMapper.insert(record);
    }

    @Override
    public int insertSelective(YlxtComplaints record) {
        return ylxtComplaintsMapper.insertSelective(record);
    }

    @Override
    public YlxtComplaints selectByPrimaryKey(Integer id) {
        return ylxtComplaintsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(YlxtComplaints record) {
        return ylxtComplaintsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(YlxtComplaints record) {
        return ylxtComplaintsMapper.updateByPrimaryKey(record);
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public ResultBody<?> pageList(QueryComplaintsDTO dto) {
        try{
            log.info("==> 开始查询投诉列表");
            PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
            LambdaQueryWrapper<YlxtComplaints> qw = new LambdaQueryWrapper<>();
            qw.eq(StringUtils.isNotEmpty(dto.getPatientId()),YlxtComplaints::getPatientId,dto.getPatientId());
            qw.eq(StringUtils.isNotEmpty(dto.getCompanionId()),YlxtComplaints::getCompanionId,dto.getCompanionId());
            qw.eq(StringUtils.isNotEmpty(dto.getComplaintDate()),YlxtComplaints::getComplaintDate,dto.getComplaintStatus());
            qw.eq(StringUtils.isNotEmpty(dto.getComplaintStatus()),YlxtComplaints::getComplaintStatus,dto.getComplaintStatus());
            qw.orderByDesc(YlxtComplaints::getCreateTime);
            List<YlxtComplaints> ylxtComplaints = ylxtComplaintsMapper.selectList(qw);
            PageInfo<YlxtComplaints> ylxtComplaintsPageInfo = new PageInfo<>(ylxtComplaints);
            log.info("==> 查询成功");
            return ResultBody.success(ylxtComplaintsPageInfo);        }catch (Exception e){
            log.error("==> 查询投诉列表失败，报错信息为{}",e.getMessage());
            return ResultBody.error("查询失败");
        }
    }

    /**
     * 解决投诉
     * @return
     */
    @Override
    public ResultBody<?> handleComplaints(HandleComplaintsDTO dto) {
        try{
            log.info("==> 开始解决投诉");
            YlxtComplaints ylxtComplaints = ylxtComplaintsMapper.selectById(dto.getId());
            ylxtComplaints.setResolutionDescription(dto.getDesc());
            ylxtComplaints.setComplaintStatus(dto.getStatus());
            ylxtComplaintsMapper.updateById(ylxtComplaints);
            return ResultBody.success();
        }catch (Exception e){
            log.error("==> 处理投诉异常");
            return ResultBody.error("处理投诉异常");
        }
    }

}
